package com.scau.os.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;


import com.scau.os.DesktopLauncher;
import com.scau.os.Launcher;
import com.scau.os.MemorySys.Hole;
import com.scau.os.extension.EventSystem;
import com.scau.os.extension.Utility;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
public class DesktopController {
    @FXML
    BorderPane borderPane;
    @FXML
    Button timeButton;
    public void initialize(){
        setWallpaper();
        updateTime();
    }

    //设置桌面壁纸
    protected void setWallpaper(){
        /**
         * 用数据流来加载图片，不会出问题
         * 反倒是用这样的方式会出问题
         *         File f=new File(Launcher.class.getResource("Image/Wallpaper.jpg").getPath());
         *         Image bg=new Image(f.getPath());
         */
        InputStream stream = Launcher.class.getResourceAsStream("Image/Wallpaper.jpg");
        Image bg = new Image(stream);

        BackgroundSize bSize = new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, true, true);
        borderPane.setBackground(new Background(new BackgroundImage(bg,
                BackgroundRepeat.REPEAT,
                BackgroundRepeat.REPEAT,
                BackgroundPosition.CENTER,
                bSize)));
    }

    //设置时钟
    protected void updateTime(){
        Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e ->
                timeButton.setText(LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")))
        ),
                new KeyFrame(Duration.seconds(1))
        );
        clock.setCycleCount(Animation.INDEFINITE);
        clock.play();
    }

    @FXML
    public void openWebsite() throws URISyntaxException, IOException {//打开浏览器
        URI uri=new java.net.URI("https://gitee.com/aaaaa1231/scau-os");
        java.awt.Desktop.getDesktop().browse(uri);
    }
    @FXML
    public void openFilesManager() throws IOException {//打开文件管理
        FXMLLoader fxmlLoader = new FXMLLoader(DesktopLauncher.class.getResource("filesManager.fxml"));
        Parent root=fxmlLoader.load();
        Scene scene = new Scene(root, 800, 600);
        Stage primaryStage=new Stage();
        primaryStage.setScene(scene);
        primaryStage.setTitle("文件管理");
        primaryStage.getIcons().add(Utility.getFxImage("Image/MyComputer.png"));
        primaryStage.show();
        filesManagerController fc=fxmlLoader.getController();
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                EventSystem.instance.UnRegisterAllBySource(fc);
            }
        });
    }

    @FXML
    public void openDiviceManager() throws IOException {//打开设备管理
        FXMLLoader fxmlLoader = new FXMLLoader(DesktopLauncher.class.getResource("diviceManager.fxml"));
        Parent root=fxmlLoader.load();
        diviceManagerController controller=fxmlLoader.getController();
        Scene scene = new Scene(root, 800, 600);
        Stage primaryStage=new Stage();
        primaryStage.setScene(scene);
        primaryStage.setTitle("设备管理");
        primaryStage.getIcons().add(Utility.getFxImage("Image/Device.png"));
        primaryStage.show();
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                EventSystem.instance.UnRegisterAllBySource(controller);
            }
        });
    }
    private LinkedList<Hole> hole_list = new LinkedList<>();
    @FXML
    public void openMemoryManager() throws IOException {//打开存储管理
        FXMLLoader fxmlLoader = new FXMLLoader(DesktopLauncher.class.getResource("memoryManager.fxml"));
        Parent root=fxmlLoader.load();
        memoryManagerController managerController= fxmlLoader.getController();
        Stage stage = new Stage();
        stage.setWidth(565);stage.setHeight(165);
        stage.setResizable(false);
        stage.setTitle("内存使用");
        Canvas canvas = managerController.canvas;
        Group group = new Group();
        group.getChildren().add(canvas);
        stage.setScene(new Scene(group));
        stage.getIcons().add(Utility.getFxImage("Image/Ram.png"));
        stage.show();
        stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                EventSystem.instance.UnRegisterAllBySource(managerController);
            }
        });
    }

    @FXML
    public void openProcessManager() throws IOException {//打开进程管理
        FXMLLoader fxmlLoader = new FXMLLoader(DesktopLauncher.class.getResource("course-view.fxml"));
        Parent root=fxmlLoader.load();
        CourseViewController controller=fxmlLoader.getController();
        AnchorPane ap=new AnchorPane(controller.outAnchorPanel);
        AnchorPane.setBottomAnchor(controller.outAnchorPanel,0.0);
        AnchorPane.setTopAnchor(controller.outAnchorPanel,0.0);
        AnchorPane.setRightAnchor(controller.outAnchorPanel,0.0);
        AnchorPane.setLeftAnchor(controller.outAnchorPanel,0.0);

        Scene scene = new Scene(ap, 960, 540);

        Stage primaryStage=new Stage();
        primaryStage.setScene(scene);
        primaryStage.setTitle("进程管理");
        primaryStage.getIcons().add(Utility.getFxImage("Image/Process.png"));
        primaryStage.show();
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                controller.unregister();
                EventSystem.instance.UnRegisterAllBySource(controller);
            }
        });
    }

    @FXML
    public void openConsole() throws IOException{
        FXMLLoader fxmlLoader = new FXMLLoader(DesktopLauncher.class.getResource("Console.fxml"));
        Parent root=fxmlLoader.load();
        Scene scene = new Scene(root, 800, 600);
        Stage primaryStage=new Stage();
        primaryStage.setScene(scene);
        primaryStage.setTitle("控制台");
        primaryStage.show();
    }

}
